<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
  <script>

    function save (code, name) {
      console.log(name)
      const URL = window.URL || window.webkitURL || window.mozURL || window.msURL
      navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob
      window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs
      let blob = new Blob([code], { type: 'application/x-pdf' })
      console.log(blob)
      if (window.saveAs) {
        window.saveAs(blob, name)
      } else if (navigator.saveBlob) {
        navigator.saveBlob(blob, name)
      } else {
        let url = URL.createObjectURL(blob)
        let link = window.document.createElement('a')
        link.setAttribute('href', url)
        link.setAttribute('download', name)
        let event = window.document.createEvent('MouseEvents')
        event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
        link.dispatchEvent(event)
      }
    }

    function getData () {
      $.ajax({
        url: '/pdftext.txt',
        method: 'get',
        type: 'json'
      }).then(data => {
        console.log(typeof data)
        save(data, 'pdf.pdf')
      })
    }
    
  </script>
  <div>
    <button id="btn" onclick="getData()">click</button>
  </div>
</body>
</html>